<?php
class user extends CI_Controller{

	public function __construct(){
		parent::__construct();
	}
	
	public function index(){
		$data['login'] = formObjects('login');
		$data['signup'] = formObjects('signup');
		$this->load->view('index',$data);
	}
    
	public function login(){
		if($this->form_validation->run('login')){
				$data = array('email'=>$this->input->post('email'),'password'=>$this->input->post('password'));
				if($this->userModel->login($data)){
					$userinfo = $this->userModel->getUserInfo('email',$data['email']);
					$this->session->set_userdata($userinfo);
					$result = json_encode($this->session->all_userdata());
				}
				else{
					$result = formError('invalid username or password');
				}
		}
		else{
			$result = formError(validation_errors());
		}
		echo $result;
	}
	
	public function signup(){
		if($this->form_validation->run('signup')){
				$data = array(
							'name'=>$this->input->post('name'),
							'email'=>$this->input->post('email'),
							'password'=>md5($this->input->post('password'))
				);
				$userinfo = $this->userModel->getUserInfo('email',$data['email']);
				$result = json_encode($userInfo);
		}
		else{
			$result = formError(validation_errors());
		}
		echo $result;
	}
	
	public function addUser(){
			$data = array(
					'name'=>$this ->security->xss_clean($this->input->post('name')),
					'email'=>$this ->security->xss_clean($this->input->post('email')),
					'password'=>md5($this ->security->xss_clean($this->input->post('password'))));
			print_r($this->input->post());
	}
	
}